package com.chandra.pos.config.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.dialect.IDialect;
import org.thymeleaf.extras.tiles2.dialect.TilesDialect;
import org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer;
import org.thymeleaf.extras.tiles2.spring4.web.view.ThymeleafTilesView;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolver;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by Admin on 6/27/14.
 */
@Configuration
public class ThymeleafConfig {


    @Bean
    public TemplateResolver templateResolver() {
        ServletContextTemplateResolver internalResourceViewResolver = new ServletContextTemplateResolver();
        internalResourceViewResolver.setPrefix("/WEB-INF/templates/");
        internalResourceViewResolver.setSuffix(".html");
        internalResourceViewResolver.setCacheable(false);
        internalResourceViewResolver.setTemplateMode("HTML5");
        return internalResourceViewResolver;
    }


    @Bean
    public ThymeleafTilesConfigurer tilesConfigurer() {
        ThymeleafTilesConfigurer tilesConfigurer = new ThymeleafTilesConfigurer();
        tilesConfigurer.setDefinitions(new String[]{"classpath*:/META-INF/*-tiles.xml"});
        return tilesConfigurer;
    }

    @Bean
    public TilesDialect tilesDialect() {
        TilesDialect dialect = new TilesDialect();
        return dialect;
    }


    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();

        engine.setTemplateResolver(templateResolver());
        Set<IDialect> dialects = new HashSet<IDialect>();

        dialects.add(tilesDialect());
        engine.setAdditionalDialects(dialects);
        return engine;
    }

    @Bean
    public ThymeleafViewResolver thymeleafViewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setViewClass(ThymeleafTilesView.class);
        resolver.setOrder(2);
        resolver.setCache(false);
        resolver.setCharacterEncoding("UTF-8");
//        String[] views = {"*.html","*.xhtml"};
//        resolver.setViewNames(views);
        return resolver;
    }


}
